package com.yt.user.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;

import com.yt.user.models.service.IIndustryService;
import com.yt.user.models.bo.IndustrySaveBO;
import com.yt.user.models.bo.IndustryBO;
import com.yt.user.models.dto.IndustryDTO;

/**
 * 行业信息控制器
 *
 * @author yzz
 * @Date 2025-10-22 09:45:39
 */

@RestController
@RequestMapping("/cms/industry")
@Slf4j
@Api(value = "IndustryController",tags = "行业信息接口")
public class IndustryController {

    @DubboReference
    private IIndustryService industryService;

    @ApiOperation(value = "添加行业信息",notes = "添加行业信息")
    @PostMapping(value = "/insert")
    public Result insert(IndustrySaveBO saveBO) {
        boolean flag = industryService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存行业信息",notes = "保存行业信息")
    public Result save(IndustrySaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改行业信息",notes = "修改行业信息")
    @PostMapping("/update")
    public Result update(IndustrySaveBO saveBO){
        IndustryDTO dto = new IndustryDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = industryService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除行业信息",notes = "删除行业信息")
    @ApiImplicitParam(name = "id",value = "行业信息id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = industryService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除行业信息",notes = "批量删除行业信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        industryService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<IndustryDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        IndustryDTO dto = industryService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "行业信息列表",notes = "行业信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<IndustryDTO>> queryPageList(IndustryBO bo){
        Wrapper<IndustryDTO> wrapper = new EntityWrapper<>();


        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<IndustryDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = industryService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
